import '../Include.dart';
import '../widget/GuideView.dart';

class SplashPage extends StatefulWidget {
  @override
  createState() => _SplashPageState();
}

class _SplashPageState extends BasePageState<SplashPage> {
  @override
  bool get isAppBar => false;

  @override
  bool get isDoubleClick => true;

  @override
  StatusBarStyle get statusBarStyle => StatusBarStyle.DARK_CONTENT;

  JvtdTimerUtils timerUtil; //计时器
  int _time = JvtdConfig.SPLASH_TIME; //倒计时时间

  @override
  void initState() {
    super.initState();
    _initTimer();
  }

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: <Widget>[
        ConstrainedBox(
          child: JvtdImageUtils.getLocalImage(
            name: JvtdImage.SPLASH_IMG,
            fit: BoxFit.fitHeight,
          ),
          constraints: new BoxConstraints.expand(), //填充父布局
        ),
        SafeArea(
          child: Align(
            alignment: Alignment.topRight,
            child: Padding(
                padding: EdgeInsets.only(right: 16, top: 16),
                child: FlatButton(
                  shape: StadiumBorder(),
                  padding: EdgeInsets.all(0),
                  color: Colors.black26,
                  onPressed: () {
                    _timeEnd();
                  },
                  child: Text(
                      JvtdString.getLocale(context).skip +
                          _time.toString() +
                          "S",
                      style: TextStyle(color: Colors.white, fontSize: 14)),
                )),
          ),
        ),
      ],
    );
  }

  //初始化计时器
  void _initTimer() {
    int sTime = 1000;
    int tTime = sTime * (_time + 1);
    if (timerUtil == null) {
      timerUtil = new JvtdTimerUtils(mInterval: sTime, mTotalTime: tTime);
    } else {
      timerUtil.setInterval(sTime);
      timerUtil.setTotalTime(tTime);
    }
    timerUtil.setOnTimerTickCallback((int value) {
      _timeUpdate();
    });

    timerUtil.startCountDown();
  }

  //取消计时器
  void _cancelTimer() {
    _time = JvtdConfig.SPLASH_TIME;
    if (timerUtil != null) timerUtil.cancel();
  }

  //时间改变
  void _timeUpdate() {
//    if(_time == 2){
//      JvtdString.changeLocale(mStore, JvtdStringType.EN);
//    }
    setState(() {
      _time--;
      if (_time < 0) {
        _time = 0;
        _timeEnd();
      }
    });
  }

  //时间改变结束，检验是否显示，界面刷新
  void _timeEnd() {
    _cancelTimer();
    _isToGuide();
  }

  //是否需要启用guide引导页
  void _isToGuide() async {
    String guideVersion =
        await JvtdLocalStorage.get(key: JvtdLocalStorage.GUIDE_VERSION);
    String currentVersion = '1';

    if (guideVersion == currentVersion) {
      _toApp();
    } else {
      JvtdLocalStorage.save(
        key: JvtdLocalStorage.GUIDE_VERSION,
        value: currentVersion,
      );
      Application.router.navigateTo(
        context,
        Routes.GUIDE_ROUTE_NAME,
        replace: true,
      );
    }
  }

  //跳转APP
  void _toApp() {
    if (JvtdConfig.IS_MUST_LOGIN) {
      //APP 强制登录  里面需要加是否已有用户操作
      Application.router.navigateTo(
        context,
        Routes.LOGIN_ROUTE_NAME,
        replace: true,
      );
    } else {
      //无需登录
      Application.router.navigateTo(
        context,
        Routes.MAIN_ROUTE_NAME,
        replace: true,
      );
    }
  }

  @override
  void dispose() {
    super.dispose();
    _cancelTimer();
  }
}
